package com.start.cms.mappers;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.cms.entity.StartCmsArticleEntity;
import com.start.cms.entity.VStartCmsArticleDetailEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface StartCmsArticleMapper extends BaseMapper<StartCmsArticleEntity> {
    String ARTICLE_DETAIL_SQL = "SELECT a.* FROM (SELECT\n" +
            "\t`sca`.`id` AS `id`,\n" +
            "\t`sca`.`article_title` AS `article_title`,\n" +
            "\t`sca`.`category_id` AS `category_id`,\n" +
            "\t`sca`.`article_tag` AS `article_tag`,\n" +
            "\t`sca`.`article_describe` AS `article_describe`,\n" +
            "\t`sca`.`article_key_world` AS `article_key_world`,\n" +
            "\t`sca`.`article_cover` AS `article_cover`,\n" +
            "\t`sca`.`article_content` AS `article_content`,\n" +
            "\t`sca`.`article_author` AS `article_author`,\n" +
            "\t`sca`.`article_source` AS `article_source`,\n" +
            "\t`sca`.`article_pv` AS `article_pv`,\n" +
            "\t`sca`.`article_uv` AS `article_uv`,\n" +
            "\t`sca`.`article_state` AS `article_state`,\n" +
            "\t`sca`.`article_release_time` AS `article_release_time`,\n" +
            "\t`sca`.`article_sort` AS `article_sort`,\n" +
            "\t`sca`.`article_del_flag` AS `article_del_flag`,\n" +
            "\t`sca`.`article_prop` AS `article_prop`,\n" +
            "\t`sca`.`create_by` AS `create_by`,\n" +
            "\t`sca`.`create_time` AS `create_time`,\n" +
            "\t`sca`.`update_by` AS `update_by`,\n" +
            "\t`sca`.`update_time` AS `update_time`,\n" +
            "\t`scc`.`category_title` AS `category_title`,\n" +
            "\t`scc`.`category_cover` AS `category_cover`,\n" +
            "\t`scc`.`category_describe` AS `category_describe`,\n" +
            "\t`scc`.`category_key_world` AS `category_key_world`,\n" +
            "\t`scc`.`category_tag` AS `category_tag`,\n" +
            "\t`scc`.`category_type` AS `category_type`,\n" +
            "\t`scc`.`category_list_template` AS `category_list_template`,\n" +
            "\t`scc`.`category_detail_template` AS `category_detail_template`,\n" +
            "\t`scc`.`category_data_model_id` AS `category_data_model_id`,\n" +
            "\t`scc`.`category_sort` AS `category_sort`,\n" +
            "\t`scc`.`parent_ids` AS `category_parent_ids` \n" +
            "FROM\n" +
            "\t `start_cms_article` `sca` JOIN `start_cms_category` `scc`  \n" +
            "WHERE\n" +
            "\t`scc`.`id` = `sca`.`category_id`\n" +
            "\t) a";

    @Select({ARTICLE_DETAIL_SQL + "  ${ew.customSqlSegment}"})
    Page<VStartCmsArticleDetailEntity> pageArticleDetail(@Param("page") Page<VStartCmsArticleDetailEntity> page, @Param(Constants.WRAPPER) Wrapper<VStartCmsArticleDetailEntity> wrapper);

    @Select({ARTICLE_DETAIL_SQL + " a.id=#{articleId}"})
    VStartCmsArticleDetailEntity articleDetailById(@Param("articleId") String articleId);

    @Update("update start_cms_article set article_pv = #{articlePv} ,article_uv=#{articleUv} where id= #{articleId}")
    Integer updateArticlePvUv(@Param("articleId") String articleId, @Param("articlePv") Integer articlePv, @Param("articleUv") Integer articleUv);
}
